package com.example.graduatetest.view.model;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.R;
import com.example.graduatetest.callback.PostCallback;
import com.example.graduatetest.databinding.ActivityCreateClassBinding;
import com.example.graduatetest.model.ClassModel;


public class CreateClassViewModel extends ViewModel {
    private Activity activity;
    private int checkedItem = 0;
    private ActivityCreateClassBinding binding;
    private String year,term;

    private MutableLiveData<String> termText;
    private MutableLiveData<String> yearText;
    private MutableLiveData<String> colorText;
    private MutableLiveData<String> nameText;
    private MutableLiveData<String> teacherText;

    public void init(Activity activity, ActivityCreateClassBinding binding){
        this.activity = activity;
        this.binding = binding;
        if(termText == null) termText = new MediatorLiveData<>();
        termText.setValue("第一学期");
        if(yearText == null) yearText = new MediatorLiveData<>();
        yearText.setValue("2020-2021");
        if(colorText == null) colorText = new MediatorLiveData<>();
        colorText.setValue("黑色");
        if(nameText == null) nameText = new MediatorLiveData<>();
        nameText.setValue(" ");
        if(teacherText == null) teacherText = new MediatorLiveData<>();
        teacherText.setValue(" ");
        binding.view.setBackgroundColor(Color.BLACK);
        toolbarInit();
        setClick();
    }

    //初始课程
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar2;
        toolbar.setTitle("编辑课程");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               activity.onBackPressed();
            }
        });

    }

    //设置点击事件
    private void setClick(){
        binding.tvTermText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               choiceTerm();
            }
        });
        binding.tvYearText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choiceYear();
            }
        });
        binding.tvColorText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choiceColor();
            }
        });
        binding.btCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(nameText.getValue().equals(" ") || teacherText.getValue().equals(" ")){
                    Toast.makeText(activity, "任课老师或课程名称为空", Toast.LENGTH_SHORT).show();
                }else{
                    year = yearText.getValue();
                    term = termText.getValue();
                    createClass(nameText.getValue(),teacherText.getValue(),year+term, colorText.getValue());
                    activity.onBackPressed();
                }
            }
        });
    }

    //classModel 网络操作
    private void createClass(String name,String teacher,String time,String color){
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
        boolean isStudent = preferences.getBoolean("isStudent", true);
        String number = preferences.getString("number", "2017211973");
        if(!isStudent){
            ClassModel.createClass(name, teacher, time, color,number, new PostCallback() {
                @Override
                public void onSuccess() {
                    Log.e("haha", "成功创建");
                }

                @Override
                public void onFailed() {
                    Log.e("haha", "创建失败");
                }
            });
        }

    }

    /**
     * 选择学期
     */
    private void choiceTerm() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);

        builder.setTitle("选择学期：");
        final String[] terms = {"第一学期", "第二学期"};

        builder.setSingleChoiceItems(terms, checkedItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                checkedItem = which;
            }
        });

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if(checkedItem == 0){
                    termText.setValue("第一学期");
                }else{
                    termText.setValue("第二学期");
                }
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog dialog = builder.create();  //创建AlertDialog对象
        dialog.show();                           //显示对话框
    }

    /**
     * 选择学年
     */
    private void choiceYear() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);

        builder.setTitle("选择学年：");
        final String[] years = {"2020-2021", "2021-2022","2022-2023"};
        builder.setSingleChoiceItems(years, checkedItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                checkedItem = which;
            }
        });

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                yearText.setValue(years[checkedItem]);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog dialog = builder.create();  //创建AlertDialog对象
        dialog.show();                           //显示对话框
    }
    /**
     * 选择颜色
     */
    private void choiceColor() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);

        builder.setTitle("选择颜色：");
        final String[] colors = {"黑色", "蓝色","黄色","红色","绿色"};
        final int[] colorFonts = {Color.BLACK,Color.BLUE,Color.YELLOW,Color.RED,Color.GREEN};
        builder.setSingleChoiceItems(colors, checkedItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                checkedItem = which;
            }
        });

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                colorText.setValue(colors[checkedItem]);
                binding.view.setBackgroundColor(colorFonts[checkedItem]);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog dialog = builder.create();  //创建AlertDialog对象
        dialog.show();                           //显示对话框
    }

    public MutableLiveData<String> getTermText() {
        return termText;
    }

    public MutableLiveData<String> getYearText() {
        return yearText;
    }

    public MutableLiveData<String> getColorText() {
        return colorText;
    }

    public MutableLiveData<String> getNameText() {
        return nameText;
    }

    public MutableLiveData<String> getTeacherText() {
        return teacherText;
    }
}
